'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2021-07-02 11:19:10
# FileName: datetime_process.py
# Software: PyCharm
'''

from dateutil import rrule
import datetime


# 算两个时间的月数
def months_calculate(begin, end):
    begin += '-01'
    end += '-01'
    d1 = datetime.datetime.strptime(begin,'%Y-%m-%d')
    d2 = datetime.datetime.strptime(end,'%Y-%m-%d')
    # d2 = datetime.date(2017, 4)
    months = rrule.rrule(rrule.MONTHLY, dtstart=d1, until=d2).count()
    return months


# 算两个时间的天数
def days_calculate(begin, end):
    begin = begin.split('-')
    end = end.split('-')
    d = int(begin[2])
    m = int(begin[1])
    y = int(begin[0])
    # difference in day
    dd = int(end[2])
    # difference in month
    dm = int(end[1])
    # difference in year
    dy = int(end[0])
    begin = datetime.date(y, m, d)
    end = datetime.date(dy, dm, dd)
    return (end - begin).days+1


# 算年数
def years_calculate(begin, end):
    begin = int(begin)
    end = int(end)
    return end-begin+1


# 生成连续的日期
def date_range(begin, end):
    ymd = "%Y-%m-%d"
    if len(begin) == 7:
        ymd = "%Y-%m"
    if len(begin) == 4:
        c = int(end) - int(begin)+1
        year = []
        for i in range(c):
            year.append(str(int(begin)+i))
        return sorted(year)
    dates = []
    dt = datetime.datetime.strptime(begin, ymd)
    date = begin[:]
    while date <= end:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime(ymd)
    return sorted(set(dates))


# 判断哪个时间大
def mix_min_check(start, end):
    a = int(start.replace('-', ''))
    b = int(end.replace('-', ''))
    if a > b:
        return False
    return True


# print(days_calculate('2021-07-03', '2021-06-29'))